import {Reducer, Effect} from 'umi'
import {message} from "antd";

import {findClassInfo, removeStu, deleteClass, updateSysClass, saveSysClass, getClassStu, assignStu} from "@/services/class"

interface ClassModelType {
  namespace: string;
  state: {
    classList: any[],
    teacherList: any[],
    classStuIds: any,
    allStuList: any[],
    // scoreMap: any[],
    userRoleId: Number,
  };
  reducers: {
    getAllList: Reducer,
    getAssignInfo: Reducer,
    initStuIds: Reducer,
  };
  effects: {
    findClassInfo: Effect,
    removeStu: Effect,
    deleteClass: Effect,
    updateSysClass: Effect,
    saveSysClass: Effect,
    getClassStu: Effect,
    initClassStuIds: Effect,
    assignStu: Effect,
  };
}


const ClassModel: ClassModelType = {
  namespace: 'classes',
  state: {
    classList: [{}],
    teacherList: [{}],
    classStuIds: undefined,
    allStuList: [{}],
    userRoleId: 0,
  },
  reducers: {
    getAllList(state, {payload}) {
      return {
        ...state,
        classList: payload.classList,
        teacherList: [...payload.teacherList],
        userRoleId: payload.userRoleId,
      }
    },
    getAssignInfo(state, {payload}) {
      return {
        ...state,
        classStuIds: payload.classStuIds,
        allStuList: payload.allStuList,
      }
    },
    initStuIds(state, {payload}) {
      return {
        ...state,
        classStuIds: undefined,
      }
    },
  },
  effects: {
    *findClassInfo({payload}, {put, call}) {
      const {code, data} = yield call(findClassInfo)
      if(code === 200) {
        yield put({
         type: 'getAllList',
         payload: data,
        })
      } else {
        message.error("尚未分配班级，请联系管理员")
      }
    },
    *removeStu({payload}, {call}) {
      const {code} = yield call(removeStu, payload)
      if(code === 200) {
        message.success("移除成功")
      } else {
        message.error("System Error")
      }
    },
    *deleteClass({payload}, {call}) {
      const {code} = yield call(deleteClass, payload)
      if(code === 200) {
        message.success("删除成功")
      } else {
        message.error("System Error")
      }
    },
    *updateSysClass({payload}, {call}) {
      const {code} = yield call(updateSysClass, payload)
      if(code === 200) {
        message.success("修改成功")
      } else {
        message.error("System Error")
      }
    },
    *saveSysClass({payload}, {call}) {
      const {code} = yield call(saveSysClass, payload)
      if(code === 200) {
        message.success("添加成功")
      } else {
        message.error("System Error")
      }
    },
    *getClassStu({payload}, {put, call}) {
      const {data} = yield call(getClassStu, payload)
      yield put({
        type: 'getAssignInfo',
        payload: data,
      })
    },
    *initClassStuIds({payload}, {put, call}) {
      yield put({
        type: 'initStuIds',
      })
    },
    *assignStu({payload}, {put, call}) {
      const {code} = yield call(assignStu, payload)
      yield put({
        type: 'initStuIds',
      })
      if(code === 200) {
        message.success("分配成功")
      } else {
        message.error("System Error")
      }
    },
  }
}

export default ClassModel
